package com.uduew.sjd

import android.content.Intent
import android.os.Bundle
import com.google.android.gms.ads.identifier.AdvertisingIdClient
import com.uduew.sjd.api.AppUpdateApi
import com.uduew.sjd.bean.HttpBean
import com.uduew.sjd.net.DataResult
import com.uduew.sjd.request.UpdateAppRequester
import com.uduew.sjd.ui.agreement.PermissionActivity
import com.uduew.sjd.ui.agreement.PrivacyActivity
import com.uduew.sjd.ui.base.BaseActivity
import com.uduew.sjd.ui.base.StateHolder
import com.uduew.sjd.utils.MainHandler
import com.uduew.sjd.utils.ToastUtils
import com.uduew.sjd.view.xpop.UpdatePromptXpop
import com.kunminx.architecture.ui.page.DataBindingConfig
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation
import com.uduew.sjd.config.AppConfig
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch

class SplashActivity : BaseActivity() {

    private lateinit var updatePromptXpop: UpdatePromptXpop
    private lateinit var mUpdateAppRequester: UpdateAppRequester
    private lateinit var mStates: SplashActivityStates

    override fun initViewModel() {
        mStates = getActivityScopeViewModel(SplashActivityStates::class.java)
        mUpdateAppRequester = getActivityScopeViewModel(UpdateAppRequester::class.java)
    }



    private fun gotoMain(time: Long) {
        if (!AppConfig.mConfigs.isPrivacy.get()) {
            startActivity(Intent(this, PrivacyActivity::class.java))
            finish()
            return
        }
        if (!AppConfig.mConfigs.isPermission.get()) {
            startActivity(Intent(this, PermissionActivity::class.java))
            finish()
            return
        }
        MainHandler.postDelay(time) {
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        }
    }

    fun initGaid() {

        val ioScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
        ioScope.launch {
            try {
                val adId = AdvertisingIdClient.getAdvertisingIdInfo(this@SplashActivity).id
                AppConfig.gaid = adId.toString()
            } catch (e: Exception) {
                e.toString()
            }

        }


    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (AppConfig.mConfigs.currentActivity().get() != null) {
            AppConfig.mConfigs.lastActivity().set(AppConfig.mConfigs.currentActivity().get())
        }
        initGaid()
        mUpdateAppRequester.result.observe(this) { dataResult: DataResult<HttpBean<AppUpdateApi.AppUpdateBean>> ->
            if (!dataResult.responseStatus.isSuccess) {
                ToastUtils.showShortToast(dataResult.responseStatus.responseCode)
                gotoMain(500)
                return@observe
            }
            dataResult.result.data.let {
                if (it != null) {
                    updatePromptXpop = UpdatePromptXpop(this, it)
                    updatePromptXpop.setOnAgreeClickListener(object :
                        UpdatePromptXpop.OnAgreeClickListener {
                        override fun onAgreeClick(anemia: String) {
//                            updateApp(anemia)
//                            gotoMain(1000)
                            openUrlInBrowser("https://www.baidu.com")

                        }

                        override fun onCancelClick(isDeorbit: Boolean) {
                            if (!isDeorbit) {
                                gotoMain(500)
                            }
                        }
                    })
                    XPopup.Builder(this)
                        .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                        .dismissOnTouchOutside(false)
                        .dismissOnBackPressed(false)
                        .asCustom(updatePromptXpop)
                        .show()
                } else {
                    gotoMain(500)
                }
            }
        }
        mUpdateAppRequester.request(this)
    }

    override fun getDataBindingConfig(): DataBindingConfig? {
        return DataBindingConfig(R.layout.activity_splash, BR.vm, mStates)
    }

    open class SplashActivityStates : StateHolder() {

    }
}